package yxy.game.pm2.command.capturestrategicpoints;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.clearancestepreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @since 2022-08-17
 * @author wrs
 * @descrition 斩关夺隘达标奖励领取（头部宝箱）
 */
@MessageType(proto = Command.CaptureStrategicPointsStandards, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsRequest.class)
public class CMD_CaptureStrategicPointsStandards extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsResponse.newBuilder();
        int id = request.getId();//宝箱的id

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
            CaptureStrategicPointsHandle handle = new CaptureStrategicPointsHandle();

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            clearancestepreward data = DataCenter.getData(id, clearancestepreward.class);
            if(data == null ){
                send(messageSource, response, Result.DataError);
                return;
            }

            ArrayList<Integer> boxMap = module.getBoxMap();
            if(boxMap.contains(data.getId())){
                send(messageSource, response, Result.CaptureStrategicPointsCantRepeatedReward);//领过
                return;
            }

            int num = handle.getPassCustomNum(module);
            if(num<data.getCondition()){
                send(messageSource, response, Result.CaptureStrategicPointsCantReward);//还不能领
                return;
            }

            //todo 领取奖励
            boxMap.add(id);
            module.set();

            Collection<Currency> cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(cur);
            new CurrencyHandle().addCurrency(playerCode, CCReason.斩关夺隘, cur);
            response.addAllItem(items);
            response.addAllBoxes(boxMap);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStandardsResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
